﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
namespace WebApplication1
{
    public class SqlHelper
    {
        private static string a = "server=.;uid=sa;pwd=123456;database=Student_db";
        private SqlConnection sql = null;

        public SqlHelper()
        {
            sql = new SqlConnection(a);
        }

        public DataTable table(string i, SqlParameter[] sqls)
        {
            try
            {
                sql.Open();
                SqlCommand sqlCommand = new SqlCommand(i, sql);
                if (sqls != null && sqls.Length!=0 )
                {
                    sqlCommand.Parameters.AddRange(sqls);
                }
                SqlDataAdapter sqlData = new SqlDataAdapter(sqlCommand);
                DataSet set = new DataSet();
                sqlData.Fill(set);
                return set.Tables[0];

            }
            catch (Exception e)
            {

                throw new Exception(e.Message);
            }
            finally 
            {
                sql.Close();
            }
        }

        public bool Execute(string i, SqlParameter[] sqls)
        {
            try
            {
                if (sql.State==ConnectionState.Closed)
                {
                sql.Open();

                }
                SqlCommand sqlCommand = new SqlCommand(i,sql);
                if (sqls != null && sqls.Length != 0)
                {
                    sqlCommand.Parameters.AddRange(sqls);

                }
                return sqlCommand.ExecuteNonQuery() > 0;
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                sql.Close();
            }

        }
    }
}